import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class PropertyDBManager extends PropertyManager {

	private Map properties;

	public PropertyDBManager(Connection connection) throws DBManagerException {
		super(connection);
		loadProperties();

	}

	@Override
	public void loadProperties() throws DBManagerException {

		Map mapProperty = new HashMap();

		ResultSet resultSet = null;

		Statement statement = null;
		boolean flag = false;
		Connection conn = getConnection(); // receive connection from
		// PropertyManager

		String query = ("SELECT Property , property_Value FROM properties");

		try {
			statement = conn.createStatement();

			resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				// Getting data from rows of Properties table
				String propertyKey = resultSet.getString("Property"); // key
				String propertyValue = resultSet.getString("property_Value"); // value

				// setting in Map data
				mapProperty.put(propertyKey, propertyValue);
			}

			flag = true;

			System.out.println("[Property] was added sucssefuly to Map");
		} catch (SQLException e) {
			throw new DBManagerException("[Property] wasn't set down in Map!");
		} finally {
			try {
				resultSet.close();
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		properties = mapProperty;
	}

	public String getPropertyByKey(String key) {

		return properties.get(key).toString();

	}

	public void updateProperty(String key, Object value) {

		properties.remove(key);
		properties.put(key, value);

	}

	public Long getNextValue(String key) {

		// Add 1 to Sequence
		updateProperty(key, (Long.parseLong(getPropertyByKey(key)) + 1));

		return Long.parseLong(getPropertyByKey(key));
	}

	public void saveProperies() {
		Set s = properties.entrySet();

		// Move next key and value of Map by iterator
		Iterator it = s.iterator();

		// receive connection from PropertyManager
		Connection conn = getConnection();
		Statement statement = null;

		try {
			statement = conn.createStatement();

			while (it.hasNext()) {

				Map.Entry m = (Map.Entry) it.next();
				String key = m.getKey().toString();
				String value = m.getValue().toString();

				String query = ("UPDATE properties SET property_value = " + value + " WHERE property = " + "'" + key + "'");

				statement.executeUpdate(query);

			}
			System.out.println("[PropertyDBManager]: Update Properties Succeed!");

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("[PropertyDBManager]: Update Properties Failed!");
			e.printStackTrace();

		} finally {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

	}

	// -----------------------------------------------------------------------
	public void printProperties() {

		Set s = properties.entrySet();

		// Move next key and value of Map by iterator
		Iterator it = s.iterator();

		while (it.hasNext()) {
			// key=value separator this by Map.Entry to get key and value
			Map.Entry m = (Map.Entry) it.next();

			// getKey is used to get key of Map
			String key = (String) m.getKey();

			// getValue is used to get value of key in Map
			double value = (Double) m.getValue();

			System.out.println("Key :" + key + "  Value :" + value);
		}

	}
}
